$(document).ready(function () {
  /**
   * @description: 预加载
   * @param {*}
   * @return {*}
   */
  var storage = window.localStorage
  var RSUId = storage.RSUId
  $.ajax({
    url: '../../rsu/getCarInside/'+RSUId,
    type: 'GET', //GET
    // data: {
    //   RsuId: RSUId
    // },
    success: function (response) {
      var html = ''
      for (var i = 0; i < response.data.cars.length; i++) {
        let vehicle_key_re = response.data.cars[i].vehicleKey.slice(0,8);
        vehicle_key_re = vehicle_key_re + "...";
        html += `<tr><td>${response.data.cars[i].vehicleId}</td> 
            <td>${response.data.cars[i].vehicleCard}</td> 
            <td>${response.data.cars[i].vehicleColor}</td> 
            <td>${response.data.cars[i].vehicleBrand}</td> 
            <td>${response.data.cars[i].vehicleType}</td> 
            <td>${vehicle_key_re}</td> 
            </tr>
            `
      }
      $('#carInfoTableT').append(html)
    },
    error: function (error) {
      response = {
        data: [
          {
            id: 1,
            vehicleCard: '浙FAEF45(测试数据，程序异常时出现)',
            vehicleColor: '蓝色',
            vehicleBrand: '林肯',
            vehicleType: 'A',
            vehicleKey: 'EF9830D3D3412'
          }
        ]
      }
      var html = ''
      for (var i = 0; i < response.data.length; i++) {
        html += `<tr><td>${response.data[i].id}</td> 
            <td>${response.data[i].vehicleCard}</td> 
            <td>${response.data[i].vehicleColor}</td> 
            <td>${response.data[i].vehicleBrand}</td> 
            <td>${response.data[i].vehicleType}</td> 
            <td>${response.data[i].vehicleKey}</td> 
            </tr>
            `
      }
      $('#carInfoTableT').append(html)
    }
  })




   /**
   * @description: 添加信息
   * @param {*}
   * @return {*}
   */
  addCarInfoBtn = function () {
    var vehicleCard = $('#inputCarCard').val()
    var vehicleColor = $('#inputCarColor').val()
    var vehicleBrand = $('#inputCarBrand').val()
    var vehicleType = $('#inputCarType').val()
    var vehicleKey = $('#inputCarKey').val()

    if (vehicleCard == '') {
      $('#carCardForm').addClass('has-error')
      $('#carCardIptDiv').append(
        " <span class='help-block'>不可为空</span>"
      )
      setTimeout(() => {
        $('#carCardForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (vehicleColor == '') {
      $('#carColorForm').addClass('has-error')
      $('#carColorIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#carColorForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (vehicleBrand == '') {
      $('#carBrandForm').addClass('has-error')
      $('#carBrandIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#carBrandForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (vehicleType == '') {
      $('#carTypeForm').addClass('has-error')
      $('#carTypeIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#carTypeForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (vehicleKey == '') {
      $('#carKeyForm').addClass('has-error')
      $('#carKeyIptDiv').append(" <span class='help-block'>不可为空</span>")
      setTimeout(() => {
        $('#carKeyForm').removeClass('has-error')
        $('.help-block').remove()
      }, 3000)
    }
    if (vehicleKey != '' && vehicleType != '' && vehicleBrand != '' && vehicleColor != '' && vehicleCard != '') {
      $.ajax({
        url: '../../vehicle/addvehicle', //引入地址
        type: 'POST', //请求方式
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(  {
          vehicle_rsuid:RSUId,
          vehicle_card: vehicleCard,
          vehicle_color: vehicleColor,
          vehicle_brand: vehicleBrand,
          vehicle_type: vehicleType,
          vehicle_key: vehicleKey,
        }),
        success: function (response) {
          $('.help-block').remove()
          if (response.isSuccess) {
            hulla.send(response.msg, 'success')
            window.location.reload()
          }
        },
        error: function (response) {
          hulla.send(response.msg, 'danger')
        }
      })
    }
  }
})
